package com.tsmti.lab.util;

import com.tsmti.basicdata.entity.DoctorInformation;
import com.tsmti.basicdata.service.DoctorInformationService;
import com.tsmti.core.common.constant.RedisConstants;
import com.tsmti.core.util.ConvertUtil;
import com.tsmti.core.util.RedisUtils;
import com.tsmti.core.util.SpringContextUtils;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package：com.tsmti.lab.util
 * @Class：DoctorUtils
 * @Description： TODO
 * @Author：dcd
 * @Date：Created in 2018/11/13 3:44 PM
 * @Company:
 * @Version：
 * @Modified By:
 */
public class DoctorUtils {
    private static Logger logger = LoggerFactory.getLogger(DoctorInformation.class);
    private static DoctorInformationService doctorInformationService = SpringContextUtils.getBean(DoctorInformationService.class);


    /**
     * 获取医生map
     * @return
     */
    public static Map<String, String> getDoctorMap() {
        Map<String, DoctorInformation> map = loadCache();
        Map<String, String> mapTemp = new HashMap<String,String>(16);
        for (String key : map.keySet()) {
            mapTemp.put(key,map.get(key).getName());
        }
        return mapTemp;
    }

    /**
     * 根据code获取姓名
     * @param code
     * @return
     */
    public static String getNameByCode(String code) {
        Map<String, DoctorInformation> allMap = loadCache();
        if (allMap.containsKey(code)) {
            return allMap.get(code).getName();
        }
        return null;
    }

    /**
     * 获取等级大于2的医生，即可开单的医生
     */
    public static Map<String, DoctorInformation> getHighLevelDoctorMap(){
        Map<String, DoctorInformation> highLevelDoctorMap = new HashedMap();
        Map<String, DoctorInformation> allMap = loadCache();
        for (String key : allMap.keySet()) {
            if (ConvertUtil.getIntValue(allMap.get(key).getJobLevel())>=2) {
                highLevelDoctorMap.put(key,allMap.get(key));
            }
        }
        return highLevelDoctorMap;
    }

    /**
     * 根据姓名获取code
     * @param name
     * @return
     */
    public static String getCodeByName(String name) {
        Map<String, DoctorInformation> allMap = loadCache();
        for (String key : allMap.keySet()) {
            if (name.equals(allMap.get(key).getName())) {
                return allMap.get(key).getCode();
            }
        }
        return null;
    }

    /**
     * 根据code获取对象
     * @param code
     * @return
     */
    public static DoctorInformation getValue(String code) {
        Map<String, DoctorInformation> allMap = loadCache();
        if (allMap.containsKey(code)) {
            return allMap.get(code);
        }
        return null;
    }


    /**
     * 加载缓存
     */
    private static Map<String, DoctorInformation> loadCache() {
        Map<String, DoctorInformation> doctorInfomationMap = new HashMap<String, DoctorInformation>(16);
        try {
            doctorInfomationMap = (Map<String, DoctorInformation>) RedisUtils.getObject(RedisConstants.DOCTORANDNURSE_CACHE);
            if (doctorInfomationMap == null) {
                doctorInfomationMap = new HashMap<String, DoctorInformation>(16);
            }
        } catch (Exception e) {
            logger.warn("读取医生护士缓存失败!");
            e.printStackTrace();
        }
        if (doctorInfomationMap == null || doctorInfomationMap.size() <= 0) {
            List<DoctorInformation> doctorInformationList = doctorInformationService.list();
            try {
                RedisUtils.setRedisToMap(RedisConstants.DOCTORANDNURSE_CACHE,"code", doctorInformationList);
            } catch (Exception e) {
                logger.warn("加载医生护士缓存错误!", e);
            }
        }
        return doctorInfomationMap;
    }
}
